package net.tp.util;

/**
 * Log algorithm steps into a string buffer.
 *
 * @author Trung Phan
 */
public class StepLogger {

    private int step;

    private StringBuilder log;

    private boolean enabled;

    public StepLogger(boolean enabled) {
        this(enabled, 0);
    }

    public StepLogger(boolean enabled, int step) {
        this.enabled = enabled;
        this.step = step;
        this.log = new StringBuilder();
    }

    public void reset() {
        if (enabled) {
            this.log = new StringBuilder();
        }
    }

    public void log(String format, Object... args) {
        if (enabled) {
            log.append(String.format(format, args));
        }
    }

    public int getStep() {
        return step;
    }

    public void incrementStep() {
        if (enabled) {
            step++;
        }
    }

    @Override
    public String toString() {
        return log.toString();
    }
}
